package com.work.order.service.impl;

import com.work.device.pojo.Device;
import com.work.device.pojo.MyDevice;
import com.work.order.dao.DeviceMapper;
import com.work.order.dao.MyDeviceMapper;
import com.work.order.dao.OrderMapper;
import com.work.order.pojo.Order;
import com.work.order.service.OrderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MyDeviceMapper myDeviceMapper;

    @Autowired
    private DeviceMapper deviceMapper;


    /**
     * 查询全部列表
     *
     * @return
     */
    @Override
    public List<Order> findAll() {
        return orderMapper.selectAll();
    }

    /**
     * 添加
     *
     * @param orderList
     */
    @Override
    public void add(List<Order> orderList) {
        if (orderList != null && orderList.size() > 0) {
            for (Order order : orderList) {
                order.setStatus("0");
                orderMapper.insertSelective(order);
            }
        }
    }

    /**
     * 修改
     *
     * @param
     */
    @Override
    public void update(Long id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        order.setStatus("1");
        MyDevice myDevice = new MyDevice();
        Device device = deviceMapper.selectByPrimaryKey(order.getDid());
        myDevice.setCid(order.getCid());
        myDevice.setDid(device.getId());
        myDevice.setCode(device.getCode());
        myDevice.setStatus("0");
        myDevice.setInstall("0");
        myDevice.setPosition("0");
        myDeviceMapper.insert(myDevice);
        orderMapper.updateByPrimaryKey(order);


    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public Integer delete(Long id) {
        //删除设备
        return orderMapper.deleteByPrimaryKey(id);
    }
}


